package com.newidea.jiangong.service.impi;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.newidea.jiangong.entity.HwHeatConverting;
import com.newidea.jiangong.mapper.HwHeatConvertingMapper;
import com.newidea.jiangong.service.HwHeatConvertingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


@Service
@Slf4j
public class HwHeatConvertingServiceImpl extends ServiceImpl<HwHeatConvertingMapper, HwHeatConverting>
        implements HwHeatConvertingService {
    @Override
    public void analysisHwOpc(JSONObject jsonObject, String number) {
        // 以前数据设置为旧数据
        HwHeatConverting updateEntity = new HwHeatConverting();
        updateEntity.setNewest(false);

        QueryWrapper<HwHeatConverting> updateWrapper = new QueryWrapper<>();
        // 是否获取最新的数据
        updateWrapper.eq("number", number);
        this.update(updateEntity, updateWrapper);


        // 获取状态数据
        JSONArray opcTag = jsonObject.getJSONArray("opcTagList");
        List<HwHeatConverting> heatConvertingList = opcTag.toList(HwHeatConverting.class).stream()
                .peek(heatConverting -> {
                    heatConverting.setNewest(true);
                    heatConverting.setNumber(number);
                }).collect(Collectors.toList());
        // 保存状态数据
        this.saveBatch(heatConvertingList);
    }
}
